<template>
  <div>
    <el-form ref="deptForm" label-width="120px" :rules="rules" :model="form">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="form.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="form.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="form.manager"
          style="width: 80%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in mangerList"
            :key="item.id"
            :value="item.username"
            :label="item.username"
          /> </el-select>==={{ form.manager }}
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="form.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments } from '@/api/departments'
export default {
  name: '',
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '', // 部门介绍
        pid: ''
      },
      mangerList: [],
      /* 校验 */
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请输入部门管理者', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  created() {
    this.loadEmp()
  },
  methods: {
    hSubmit() {
      this.$refs['deptForm'].validate(async(valid) => {
        if (valid) {
          // alert('submit!')
          //  5-2 兜底校验满足成功后执行
          const res = await addDepartments(this.form)
          console.log(res, 66)
          // 5-2-3 添加部门成功后
          if (res.code === 10000) {
            // 关闭对话框+清空表单数据+刷新页面
            this.$emit('update:dialogFormVisible', false)
            this.form = {
              name: '', // 部门名称
              code: '', // 部门编码
              manager: '', // 部门管理者
              introduce: '', // 部门介绍
              pid: ''
            }
            // 刷新页面
            this.$emit('success')
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    hCancel() {},
    async loadEmp() {
      const res = await getEmployeeSimple()
      // console.log(res, 5)
      this.mangerList = res.data
    }
  }
}
</script>
<style lang="less" scoped></style>
